/*
* Main file of tedi2md
* Copyright (C) <2022>  <alkeon> [alkeon@autistici.org]
   
* Texdi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Texdi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tedi2md.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include "../tedi2lang/tedi2lang.h"
#include "tedi2md.h"
#include "../tedi2lang/exception.h"

using namespace std;

#define BLOCK 2
#define IMAGE 1
#define LINK 0
#define NO_TAG -1

tedi2md::tedi2md(tags_definition td): tedi2lang(td){}

tedi2md::tedi2md(): tedi2lang(default_tags_value()){}

tags_definition tedi2md::default_tags_value(){
	
	tags_definition td;

	td.start_heading_first_level_tag = "# ";
	td.start_heading_second_level_tag = "## ";
	td.start_heading_third_level_tag = "### ";
	td.start_heading_fourth_level_tag = "#### ";

	td.end_heading_first_level_tag = "";
	td.end_heading_second_level_tag = "";
	td.end_heading_third_level_tag = "";
	td.end_heading_fourth_level_tag = "";

	td.start_list_tag = "";
	td.list_item_tag = "- ";
	td.end_list_tag = "";

	td.start_container_tag = "<div class=\"";
	td.middle_container_tag = "\">";
	td.end_container_tag = "</div>";

	td.start_link_tag = "<a href=\"";
	td.middle_link_tag = "\">";
	td.end_link_tag = "</a>";

	td.start_image_tag = "<img alt=\"";
	td.middle_image_tag = "\" src=\"";
	td.end_image_tag = "\">";

	td.start_table_tag = "<table>";
	td.end_table_tag = "</table>";
	td.start_table_row_tag = "<tr>";
	td.end_table_row_tag = "</tr>";
	td.start_table_data_tag = "<td>";
	td.end_table_data_tag = "</td>";

	return td;
}


/*
 * Control tags:
 * <!, <>, <+. <
 */
string tedi2md::convert_line_control_tags(string& line){
	if(line[1] != '!') {
		if(line[1] == '>') {
			line = line.erase(0,2);
			return line + "\n";
		} else if(line[1] == '+') {
			line = line.erase(0,2);
			if(_is_paragraph) {
				_is_paragraph = false;
				return line + _end_paragraph_tag + "\n";

			} else
				return _start_paragraph_tag + line + _end_paragraph_tag + "\n";
			
		} else {
			line = line.erase(0,1);
			return line + "\n";
		}
	} else
		return "";
	
}
